﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.XPath;
using System.Xml;

namespace ServBus.ListForm2010.Utility
{
    public class XmlUtility
    {
        public static XPathNavigator CreateDocNav(string xml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xml));
            XPathNavigator nav = xmlDoc.CreateNavigator();
            nav.MoveToFirstChild();
            return nav;
        }

        public static XmlElement CreateXMLElement(string xml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xml));
            XmlElement root = xmlDoc.DocumentElement;
            return root;
        }

        //public static void AddDataObjectFile(string name, FieldInfo field, string fileName)
        //{
        //    string content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
        //                      + "<xsd:schema targetNamespace=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:dms=\"http://schemas.microsoft.com/office/2009/documentManagement/types\" xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" xmlns:q=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields\" xmlns:d=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields\" xmlns:ma=\"http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes\" xmlns:pc=\"http://schemas.microsoft.com/office/infopath/2007/PartnerControls\">"
        //                      + "<xsd:import namespace=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields\" schemaLocation=\"lookup1_df.xsd\"/>"
        //                      + "<xsd:import namespace=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields\" schemaLocation=\"lookup1_qf.xsd\"/>"
        //                      + "<xsd:element name=\"myFields\">"
        //                      + "<xsd:complexType>"
        //                      + "<xsd:sequence>"
        //                      + "<xsd:element name=\"queryFields\">"
        //                      + "<xsd:complexType>"
        //                      + "<xsd:sequence>"
        //                      + "<xsd:element ref=\"q:SharePointListItem_RW\" minOccurs=\"0\" maxOccurs=\"1\"/>"
        //                      + "</xsd:sequence>"
        //                      + "</xsd:complexType>"
        //                      + "</xsd:element>"
        //                      + "<xsd:element name=\"dataFields\">"
        //                      + "<xsd:complexType>"
        //                      + "<xsd:sequence>"
        //                      + "<xsd:element ref=\"d:SharePointListItem_RW\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
        //                      + "</xsd:sequence>"
        //                      + "</xsd:complexType>"
        //                      + "</xsd:element>"
        //                      + "<xsd:any namespace=\"##other\" processContents=\"lax\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
        //                      + "</xsd:sequence>"
        //                      + "<xsd:anyAttribute namespace=\"##other\" processContents=\"lax\"/>"
        //                      + "</xsd:complexType>"
        //                      + "</xsd:element>"
        //                      + "</xsd:schema>";

        //    //File.Create(fullName);
        //    using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
        //    {
        //        StreamWriter sw = new StreamWriter(fs);
        //        sw.Write(content);
        //        sw.Flush();
        //    }
        //}

        //public static void AddDataObjectDFSchemaFile(string dataObjName, FieldInfo field, string fileName)
        //{
        //    FileStream fs = new FileStream(NameSpaceConstants.TemplateRootPath + "\\" + "lookup1_df.xsd", FileMode.Open, FileAccess.Read);
        //    StreamReader sr = new StreamReader(fs);
        //    string s = sr.ReadLine();

        //    FileStream fs1 = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        //    StreamWriter sw = new StreamWriter(fs1);
        //    while (!String.IsNullOrEmpty(s))
        //    {
        //        sw.WriteLine(s.Replace("{0}", field.Name).Replace("{1}", field.DisplayName));
        //        s = sr.ReadLine();
        //    }

        //    sw.Flush();

        //    fs.Close();
        //    fs1.Close();
        //}

        //public static void AddDataObjectQFSchemaFile(FieldInfo field, string path)
        //{
        //    FileStream fs = new FileStream(NameSpaceConstants.TemplateRootPath + "\\" + "lookup1_qf.xsd", FileMode.Open, FileAccess.Read);
        //    StreamReader sr = new StreamReader(fs);
        //    string s = sr.ReadLine();

        //    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);
        //    StreamWriter sw = new StreamWriter(fs1);
        //    while (!String.IsNullOrEmpty(s))
        //    {
        //        sw.WriteLine(s.Replace("{0}", field.Name).Replace("{1}", field.DisplayName));
        //        s = sr.ReadLine();
        //    }

        //    sw.Flush();

        //    fs.Close();
        //    fs1.Close();

        //}
    }
}